<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Auth extends Controller_Admin_Admin {

  /**
   * @var string
   */
  public $success_url = '/admin';

  /**
   * User Login Action
   */
  public function action_login()
  {
    // create template
    $template = View::factory('admin/auth/login');

    // init errors array
    $errors = array();

    // check request method
    if ($this->request->method() == Request::POST)
    {
      // validate user data
      $post = Validation::factory($this->request->post())
        ->rule('email', 'not_empty')
        ->rule('password', 'not_empty')
        ->labels(array(
          'login' => 'User email',
          'password' => 'Password',
        ));

      // if data valid
      if ($post->check())
      {
        // try login user
        if (Auth::instance()->login($post['email'], $post['password'], isset($post['remember'])))
        {
          Controller::redirect($this->success_url);
        }

        // set error
        $errors = array('common' => 'Invalid email or password');
      }

      // add validation errors
      $errors = Arr::merge($errors, $post->errors('messages'));
    }

    // set variables into login template
    $template->set('errors', $errors);

    // set content into template
    $this->template->set('content', $template);
  }

  /**
   * User Logout Action
   */
  public function action_logout()
  {
    // logout
    Auth::instance()->logout(TRUE, TRUE);

    // redirect
    Controller::redirect('admin');
  }

  /**
   * User Initialize Action
   */
  public function action_init()
  {
    // find current admin user
    $user = ORM::factory('User', array('username' => 'admin'));

    // if user not founded
    if ($user->loaded() === FALSE)
    {
      // create new admin user
      $user->values(array(
        'password' => '121212',
        'username' => 'admin',
        'email' => 'admin@megastroy.com',
      ))->save();

      // add roles for admin user
      $user->add('roles', ORM::factory('Role', array('name' => 'login')));
      $user->add('roles', ORM::factory('Role', array('name' => 'admin')));

      // user message
      $this->template->set('content', '<h1>Done!</h1>');

      // exit
      return ;
    }

    // user message
    $this->template->set('content', '<h1>Error!</h1>');
  }

} // End Admin Default